﻿import java.io.*;
import java.util.*;

public class RestaurantReservationSystem {

    // 菜单选项
    private static final int USER_OPTION = 1;
    private static final int MERCHANT_OPTION = 2;
    private static final int ADMIN_OPTION = 3;
    private static final int EXIT_OPTION = 4;

    // 预定状态
    private static final String BOOKED_STATUS = "已预订";
    private static final String AVAILABLE_STATUS = "可用";

    // 菜品列表
    private static List<Dish> dishList = new ArrayList<>();

    // 预订列表
    private static List<Reservation> reservationList = new ArrayList<>();


    public static void main(String[] args) {
        boolean b = true;
        Scanner sc = new Scanner(System.in);
        UserSystem userSystem = new UserSystem();
        userSystem.neizhi();
        System.out.println("请输入您的账号密码：");
        String username = sc.next();
        String password = sc.next();
        if (!userSystem.login(username, password)) {
            b = false;
            System.out.println("您输入的账号密码不正确，请重新输入");
            String username2 = sc.next();
            String password2 = sc.next();
            if (!userSystem.login(username, password)) {
                System.out.println("你输入的账号密码多次错误，请重新注册您的账号密码：");
                String username3 = sc.next();
                String password3 = sc.next();
                userSystem.register(username3, username3);
            }
        } else {
            b = true;
        }
        ;

        Scanner scanner = new Scanner(System.in);

        // 初始化菜品列表
        initDishList();

        // 主程序循环
        while (b) {

            System.out.println("请选择您的身份：");
            System.out.println("1. 用户");
            System.out.println("2. 商家");
            System.out.println("3. 后台管理员");
            System.out.println("4. 退出系统");

            int option = scanner.nextInt();

            switch (option) {
                case USER_OPTION:
                    userOption(scanner);
                    break;
                case MERCHANT_OPTION:
                    merchantOption(scanner);
                    break;
                case ADMIN_OPTION:
                    adminOption(scanner);
                    break;
                case EXIT_OPTION:
                    System.out.println("感谢使用餐厅预定管理系统！");
                    return;
                default:
                    System.out.println("无效的选项，请重新选择！");
            }
        }
    }

    // 用户选项
    private static void userOption(Scanner scanner) {


        while (true) {
            System.out.println("请选择您要进行的操作：");
            System.out.println("1. 附近商家热度排名查看");
            System.out.println("2. 浏览菜单");
            System.out.println("3. 预订座位");
            System.out.println("4. 取消预订");
            System.out.println("5. 返回上一级菜单");

            int option = scanner.nextInt();

            switch (option) {
                case 1:
                    viewDishList();
                    break;
                case 2:
                    bookSeat(scanner);
                    break;
                case 3:
                    cancelReservation(scanner);
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效的选项，请重新选择！");
            }
        }
    }

    // 后台管理员选项
    private static void adminOption(Scanner scanner) {

        while (true) {

            System.out.println("请选择您要进行的操作：");
            System.out.println("1. 添加新的菜品");
            System.out.println("2. 更新现有的菜品信息");
            System.out.println("3. 返回上一级菜单");

            int option = scanner.nextInt();

            switch (option) {
                case 1:
                    addDish(scanner);
                    break;
                case 2:
                    updateDish(scanner);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效的选项，请重新选择！");
            }
        }
    }

    // 初始化菜品列表
    private static void initDishList() {

        dishList.add(new Dish("烤鸭", 68));
        dishList.add(new Dish("红烧肉", 38));
        dishList.add(new Dish("清蒸鲈鱼", 58));
        dishList.add(new Dish("宫保鸡丁", 28));
    }

    // 浏览菜单
    private static void viewDishList() {

        System.out.println("菜单如下：");

        for (Dish dish : dishList) {
            System.out.println(dish.toString());
        }
    }

    // 预订座位
    private static void bookSeat(Scanner scanner) {
        Reservation reservation1 = new Reservation(AVAILABLE_STATUS);
        reservationList.add(reservation1);

        System.out.println("请输入您的姓名：");
        String name = scanner.next();

        int seatCount = scanner.nextInt();

        boolean available = checkAvailability(seatCount);

        if (available) {
            for (Reservation reservation : reservationList) {
                if (reservation.getStatus().equals(AVAILABLE_STATUS)) {
                    reservation.setStatus(BOOKED_STATUS);
                    reservation.setUserName(name);
                    reservation.setSeatCount(seatCount);
                    break;
                }
            }

            System.out.println("预订成功！");
        } else {
            System.out.println("座位不足，请重新选择！");
        }
    }

    // 取消预订
    private static void cancelReservation(Scanner scanner) {

        System.out.println("请输入您的姓名：");
        String name = scanner.next();

        for (Reservation reservation : reservationList) {
            if (reservation.getUserName().equals(name)) {
                reservation.setStatus(AVAILABLE_STATUS);
                reservation.setUserName("");
                reservation.setSeatCount(0);
                break;
            }
        }

        System.out.println("取消预订成功！");
    }

    // 检查是否有足够的座位
    public static int availableCount = 10;

    private static boolean checkAvailability(int seatCount) {
//        for (Reservation reservation : reservationList) {
//            if (reservation.getStatus().equals(AVAILABLE_STATUS)) {
//                availableCount += reservation.getSeatCount();
//            }
//        }
        if (availableCount >= seatCount && (availableCount - seatCount >= 0)) {
            availableCount -= seatCount;
            return true;
        }
        return false;
    }

    // 添加新的菜品
    private static void addDish(Scanner scanner) {

        System.out.println("请输入新的菜品名称：");
        String name = scanner.next();

        System.out.println("请输入新的菜品价格：");
        int price = scanner.nextInt();

        dishList.add(new Dish(name, price));

        System.out.println("添加菜品成功！");
    }

    // 更新现有的菜品信息
    private static void updateDish(Scanner scanner) {

        System.out.println("请选择要更新的菜品序号：");

        for (int i = 0; i < dishList.size(); i++) {
            System.out.println(i + 1 + ". " + dishList.get(i).toString());
        }

        int index = scanner.nextInt() - 1;

        System.out.println("请输入新的菜品名称：");
        String name = scanner.next();

        System.out.println("请输入新的菜品价格：");
        int price = scanner.nextInt();

        dishList.set(index, new Dish(name, price));

        System.out.println("更新菜品信息成功！");
    }

    // 商家选项
    private static void merchantOption(Scanner sc) {

        while (true) {

            System.out.println("请选择您要进行的操作：");
            System.out.println("1. 查看用户预定的座位信息");
            System.out.println("2. 商家排名查看");
            System.out.println("3. 返回上一级菜单");

            int option = sc.nextInt();

            switch (option) {
                case 1:
                    reserve();
                    break;
                case 2:
                    rankView();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效的选项，请重新选择！");
            }
        }
    }

    public static void reserve() {
        System.out.println("所有预订信息如下：");

        for (Reservation reservation : reservationList) {
            System.out.println(reservation.toString());
        }
    }

    public static void rankView() {
        HashMap<Integer, String> map = new HashMap<>();

        String[] str = new String[10];

        str[0] = "麦当劳";
        str[1] = "KFC";
        str[2] = "米雪冰城";
        str[3] = "古茗";
        str[4] = "汉堡王";
        str[5] = "牛油拌饭";
        str[6] = "北京烤鸭";
        str[7] = "每日邮堡";
        str[8] = "茶百道";
        str[9] = "牛约堡";

        int i = 1;
        for (String s : str) {
            map.put(i,s);
            i++;
        }

        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            Integer key = entry.getKey();
            String val = entry.getValue();
            System.out.println("附近商家的总排名情况如下：" );
            System.out.println("排名第" + key + "的商家为：" + val);
        }

    }
}

